<?php namespace App\Http\Controllers;

use App\Example;
use App\Exceptions\SecurityException;
use App\Facilitator;
use App\Msg;
use App\Org;
use App\Sys;
use App\User;
use Illuminate\Auth\Guard;
use Illuminate\Support\Facades\Input;

class AdminController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
//		$this->middleware('auth');
    }

    public function login(Guard $guard){
        if($guard->user())
            return redirect(route('Admin.Index.index'));

        return view('admin.login');
    }

    public function index(Guard $guard){
        if($guard->guest())
            return redirect(route('Admin.Auth.login'));

        /** @var User $user */
        $user = $guard->user();
        if((int)$user->role !== 3){
            return redirect(route('Home'));
        }

        $users = User::where('role','<','3')->paginate(10);

        return view('admin.index',['users'=>$users]);
    }

    /** 登出 */
    public function signOut(Guard $guard)
    {
        $guard->logout();
        return redirect(route('Admin.Auth.login'));
    }

    public function facListPage(Guard $guard){
        if($guard->guest())
            return redirect(route('Admin.Auth.login'));

        /** @var User $user */
        $user = $guard->user();

        $facs = Facilitator::orderBy('created_at', 'desc')->paginate(10);

        return view('admin.users.facslist',['facs'=>$facs]);
    }

    public function orgListPage(Guard $guard){
        if($guard->guest())
            return redirect(route('Admin.Auth.login'));

        /** @var User $user */
        $user = $guard->user();


        $orgs = Org::orderBy('created_at', 'desc')->paginate(10);

        /** @var Org $orgInfo */
        foreach($orgs as $orgInfo){
            if($orgInfo){
                $field = $orgInfo->fields()->get();
                $orgInfo['fields'] = $field;
            }

            if($orgInfo){
                $skill = $orgInfo->skills()->get();
                $orgInfo['skills'] = $skill;
            }
        }

        return view('admin.users.orglist',['orgs'=>$orgs]);
    }

    public function inviteListPage(Guard $guard){
        if($guard->guest())
            return redirect(route('Admin.Auth.login'));

        /** @var User $user */
        $user = $guard->user();

        $facs = Facilitator::orderBy('created_at', 'desc')->paginate(10);

        return view('admin.users.invitelist',['facs'=>$facs]);
    }

    public function managerListPage(Guard $guard){
        if($guard->guest())
            return redirect(route('Admin.Auth.login'));

        /** @var User $user */
        $user = $guard->user();

        $admins = User::whereRole('3')->orderBy('created_at', 'desc')->paginate(10);

        return view('admin.users.managerlist',['admins'=>$admins]);
    }

    public function examListPage(Guard $guard){
        $orgId = Input::get('orgid');
        if($guard->guest())
            return redirect(route('Admin.Auth.login'));

        /** @var User $user */
        $user = $guard->user();


        $exams = Example::orderBy('created_at', 'desc')->paginate(10);

        if($orgId){
            $exams = Example::whereOrgId($orgId)->orderBy('created_at', 'desc')->paginate(10);
        }

        return view('admin.users.examlist',['exams'=>$exams]);
    }

    public function checkFac(Guard $guard,$id){
        if($guard->guest())
            return redirect(route('Admin.Auth.login'));

        /** @var User $user */
        $user = $guard->user();

        $fac = Facilitator::find($id);

        return view('admin.users.edit_facs',['fac'=>$fac]);
    }

    public function checkOrg(Guard $guard,$id){
        if($guard->guest())
            return redirect(route('Admin.Auth.login'));

        /** @var User $user */
        $user = $guard->user();

        /** @var Org $orgInfo */
        $orgInfo = Org::find($id);

        if($orgInfo){
            $field = $orgInfo->fields()->get();
            $orgInfo['fields'] = $field;
        }

        if($orgInfo){
            $skill = $orgInfo->skills()->get();
            $orgInfo['skills'] = $skill;
        }

        return view('admin.users.edit_org',['org'=>$orgInfo]);
    }

    public function checkInvite(Guard $guard,$id){
        if($guard->guest())
            return redirect(route('Admin.Auth.login'));

        /** @var User $user */
        $user = $guard->user();

        $fac = Facilitator::find($id);

        return view('admin.users.edit_invite',['fac'=>$fac]);
    }

    public function checkExam(Guard $guard,$id){
        if($guard->guest())
            return redirect(route('Admin.Auth.login'));

        /** @var User $user */
        $user = $guard->user();

        $exam = Example::find($id);

        return view('admin.users.edit_exam',['exam'=>$exam]);
    }

    public function showChangePwd(Guard $guard,$id){
        if($guard->guest())
            return redirect(route('Admin.Auth.login'));

        /** @var User $user */
        $user = User::find($id);
        return view('admin.users.change_password',['user'=>$user]);
    }

    public function showMessagePage(Guard $guard){
        if($guard->guest())
            return redirect(route('Admin.Auth.login'));

        $msgs = Msg::orderBy('created_at', 'desc')->paginate(10);
        return view('admin.users.messagelist',['msgs'=>$msgs]);
    }

    public function showSysInfoPage(Guard $guard){
        if($guard->guest())
            return redirect(route('Admin.Auth.login'));

        $sysinfo = Sys::find(1);
        return view('admin.users.edit_system',['sysinfo'=>$sysinfo]);
    }
}